﻿using Microsoft.Win32;
using SHKF.B2C.Mannage.AdminUI.Controls;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Dto.Center.ProductBaseImage;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.ProductBase
{
    /// <summary>
    /// frmProductBaseEdit.xaml 的交互逻辑
    /// </summary>
    public partial class frmProductBaseEdit : BasePage
    {
        public frmProductBaseEdit()
        {
            InitializeComponent();
        }

       
        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadImage();
        }

        private async void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (base.Verifiy())
            {
                ProductBaseEntity data = (ProductBaseEntity)this.DataContext;
                ProductBaseDto dto = await GL.HttpClientManage.ProductBaseSave(data);
                if (dto.error == null)
                {
                    this.DataContext = dto.data;
                    MessageBox.Show("保存成功!");
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }
        }
         
        private async void LoadImage()
        {
            foreach (ProductBaseImage productBaseImage in this.imageList.Children)
            {
                productBaseImage.ClickAdd += ProductBaseImage_ClickAdd;
                productBaseImage.ClickDelete += ProductBaseImage_ClickDelete;
                productBaseImage.MouseLeftButtonDown += ProductBaseImage_MouseLeftButtonDown;
                productBaseImage.DataContext = null;
            }

            ProductBaseEntity data = (ProductBaseEntity)this.DataContext;
            if (data != null && data.SN > 0)
            {
                ProductBaseImageListDto dto = await GL.HttpClientManage.ProductBaseImageQuery(data.SN);
                if( dto.error == null)
                {
                    foreach (ProductBaseImage productBaseImage in this.imageList.Children)
                    {
                        int index = productBaseImage.Index;
                        ProductBaseImageEntity image = dto.data.Find(ww => ww.Index == index);
                        productBaseImage.DataContext = image;
                    }
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }
        }
 

        private async void ProductBaseImage_ClickAdd(object sender, RoutedEventArgs e)
        {
            ProductBaseEntity data = (ProductBaseEntity)this.DataContext;
            if(data.SN > 0)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == true)
                {
                    ProductBaseImage control = (ProductBaseImage)sender;
                     
                    Stream stream = openFileDialog.OpenFile();
                    byte[] beffer = new byte[stream.Length];
                    stream.Read(beffer, 0, beffer.Length);
                    stream.Close();
                    ProductBaseImageEntity info = new ProductBaseImageEntity();
                    info.FKSN = data.SN;
                    info.Folder = data.ProductName;
                    info.ImageName = info.Folder + control.Title;
                    info.Index = control.Index;
                    info.Picture = beffer;

               
                    ProductBaseImageDto dto = await GL.HttpClientManage.ProductBaseImageAdd(info);
                    if (dto.error == null)
                    {
                        control.DataContext = dto.data;
                    }
                    else
                    {
                        MessageBox.Show(dto.error.msg);
                    }
                }
            }
            else
            {
                MessageBox.Show("请先维护商品数据");

            }
        }

        private async void ProductBaseImage_ClickDelete(object sender, RoutedEventArgs e)
        {
            ProductBaseImage control = (ProductBaseImage)sender;
            ProductBaseImageEntity data = (ProductBaseImageEntity)control.DataContext;
            if(data != null)
            {
                DataDto dto =  await GL.HttpClientManage.ProductBaseImageDelete(data);
                if(dto.error == null)
                {
                    control.DataContext = null;
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }

        }

        private void ProductBaseImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ProductBaseImage control = (ProductBaseImage)sender;
            if(control.DataContext != null)
            {
                ProductBaseImagePreview win = new ProductBaseImagePreview();
                win.SetImage(control.GetImageSource());
                win.ShowDialog();
               
            }
        }
    }
}
